/*
Copyright (c) 2009 Ben Beaumont

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. 
*/
package com.dogcatfishdish.utils
{
	/**
	 * Utility class for manupulating objects in 3D space. 
	 *  
	 * @author benbeaumont
	 */	
	public class Geom3DUtils
	{
						
		static private var _pointsCacheGoldenSpiral:Array = [];
		static private var _pointsCacheSaffKuijlaars:Array = [];
		
		static public const GOLDEN_SPIRAL:String = 'golden_spiral';
		static public const SAFF_KUIJLAARS:String = 'saff_kuijlaars';
		
		/**
		 * Creates an Array cartesian points aroud the surface of a sphere. 
		 * Think of the surface of a goldball and your there.
		 * 
		 * @param numberOfPoints The number of points to be distributed on the surface.
		 * @param method The distibution algorithm. Defaults to golden spiral, but it doesn't make a huge amount 
		 * of difference which method you use.
		 * @return Array of Objects instances with properties x, y and z.
		 * 
		 */		
		static public function pointsOnSphere(numberOfPoints:uint, method:String = ''):Array
		{
			switch(method)
			{
				case SAFF_KUIJLAARS :
					return pointsOnSphereGoldenSpiral(numberOfPoints);
				case GOLDEN_SPIRAL:
				default:
					return pointsOnSphereSaffKuijlaars(numberOfPoints);
			}
		}
		
		/**
		 * Returns an array of Number3D points evenly distributed around the surface of a sphere using a spiral system. 
		 * Values are normalised to a radius of 1. This uses the Golden Spiral method
		 * 
		 * @param numberOfPoints The number of points required
		 *
		 * return Array of Objects instances with properties x, y and z.
		 */		
		static protected function pointsOnSphereGoldenSpiral(numberOfPoints:uint):Array
		{
			var cached:Array = _pointsCacheGoldenSpiral[numberOfPoints];
							
			if(cached != null)
				return cached.slice();

			var points:Array = [];
			
			var inc:Number = Math.PI * (3 - Math.sqrt(5));
			var offset:Number = 2 / numberOfPoints;
			var y:Number, r:Number, phi:Number;
			
			for(var i:uint = 0; i < numberOfPoints; ++i)
			{
				y = i * offset - 1 + (offset * 0.5);
				r = Math.sqrt(1 - y * y);
				phi = i * inc;
				
				points[points.length] = {x: Math.cos(phi) * r, y: y, z: Math.sin(phi) * r};
			}
			
			_pointsCacheGoldenSpiral[numberOfPoints] = points;
			
			return points;
		}
		
		/**
		 * Returns an array of Number3D points evenly distributed around the surface of a sphere using a spiral system. 
		 * Values are normalised to a radius of 1. This uses the Saff Kuijlaars method, which isn't much different.
		 * 
		 * @param numberOfPoints The number of points required
		 *
		 * @return Array of Objects instances with properties x, y and z.
		 */	
		static protected function pointsOnSphereSaffKuijlaars(numberOfPoints:uint):Array
		{
			var cached:Array = _pointsCacheSaffKuijlaars[numberOfPoints];
							
			if(cached != null)
				return cached.slice();
				
			var points:Array = [];
			
			var s:Number = 3.6 / Math.sqrt(numberOfPoints);
			var phi:Number = 0;
			var y:Number, r:Number;
			
			points[points.length] = {x: 0, y: -1, z: 0};
			
			for(var i:uint = 1; i < numberOfPoints - 1; ++i)
			{
				y = -1 + 2 * i / (numberOfPoints - 1);
				r = Math.sqrt(1 - y * y);
				phi = phi + s / r;
				
				points[points.length] = {x: Math.cos(phi) * r, y: y, z: Math.sin(phi) * r};
			}
			
			points[points.length] = {x: 0, y: 1, z: 0};
			
			_pointsCacheSaffKuijlaars[numberOfPoints] = points;
			
			return points;
		}
	}
}